import javax.microedition.io.StreamConnection;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;


class PointerCanvas extends Canvas implements javax.microedition.lcdui.CommandListener{
	MIDlet midlet;
	J2meSender sender;
	String eventType = "Press Pointer!";
	int x=0;
	int y=0;
	int relx=0;
	int rely=0;
	boolean fullscreen=false;
	boolean clickedonscroll=false;
	boolean leftispressed=false;
	boolean rightispressed=false;
	int scrollpercent=15;
	int fullscreenpercent=10;
	int failratio=10;

	Command right = new Command("Click Right", Command.SCREEN,2);
	Command left = new Command("Left", Command.SCREEN,1);
	Command exit = new Command("Exit", Command.SCREEN,9);
	Command pressleft = new Command("Press Left", Command.BACK,2);
	Command releaseleft = new Command("Release Left", Command.BACK ,1);
	Command pressright = new Command("Press Right", Command.SCREEN,2);
	Command releaseright = new Command("Release Right", Command.BACK ,1);

public PointerCanvas(MIDlet midlet2, int port, String ip, StreamConnection c){
	midlet=midlet2;
	sender=new J2meSender(c, port, ip);
	// Añadimos el comando Salir e indicamos que clase lo manejará
    this.addCommand(right);
    //this.addCommand(left);
    this.addCommand(exit);
    this.addCommand(pressright);
    this.addCommand(pressleft);
    this.setCommandListener(this);
}

  public void pointerPressed (int x, int y) {
    eventType = "Pointer Pressed";
    this.x = x;
    this.y = y;
    //control de la pantalla completa
    if (x >= (getWidth()/100.0*(100-scrollpercent))){
    	clickedonscroll=true;
    }
    this.relx = x;
    this.rely = y;
    repaint ();
  }

  public void pointerReleased (int x, int y) {
    eventType = "Pointer Released";
    if(clickedonscroll){
    	clickedonscroll=false;
    }
    //clicked
    if ((Math.abs(x-this.relx) <= failratio )||(Math.abs(y-this.rely)<=failratio)){
		clicked(x, y);
    }
    this.x = x;
    this.y = y;
    sender.flush();
    //repaint ();
  }

  public void pointerDragged (int x, int y) {

    if((clickedonscroll)&&((Math.abs(x-this.x) >= failratio)||(Math.abs(y-this.y) >= failratio))){
    	send("scroll " + (y-this.y));
		this.x = x;
		this.y = y;
    }if((Math.abs(x-this.x) >= failratio)||(Math.abs(y-this.y) >= failratio)){
		send("move " + (x-this.x)+" "+(y-this.y));
		this.x = x;
		this.y = y;
	}
    //repaint ();
  }

  public void paint (Graphics g) {
	  	//fondo
	  	g.setColor(47,79,79);
	    g.fillRoundRect (2, 2, (getWidth ()-4), (getHeight()-4), 50, 50);
	    //Scroll
	    g.setColor(46,139,87);
	    g.fillRoundRect((int)(getWidth()/100.0*(100-scrollpercent)-8), (8), (int)(getWidth()/100.0*scrollpercent-8), (getHeight ()-16), 25, 50);

	    //fullscreen
	    g.setColor(250,128,114);
	    g.fillTriangle(0, 0, 0, (int)(getHeight()/100.0*fullscreenpercent+1), (int)(getHeight()/100.0*fullscreenpercent+1), 0);
	    g.setColor(0,0,0);
	    g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM));
	    if(fullscreen){
		    g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_UNDERLINED, Font.SIZE_MEDIUM));
	    }
	    //g.drawString ("F", 4, 0, Graphics.TOP|Graphics.LEFT);

	    //titulo

	    g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE));
	    g.setColor(46,139,87);
	    g.drawString ("Remote Touch Pad",
	           15, getHeight()-30, Graphics.TOP|Graphics.LEFT);
	    //g.drawLine (x-4, y, x+4, y);
	    //g.drawLine (x, y-4, x, y+4);
  }

public void commandAction(Command c, Displayable d) {
	// TODO Auto-generated method stub
	  // Salir
	String mensaje1="move 500 500";
	int key=getCode(c);
	switch (key) {
	case 1:	//left click
		this.removeCommand(releaseleft);
	    this.addCommand(pressleft);
		mensaje1="click 1";
		break;
	case 2:	//right click
		this.removeCommand(releaseright);
	    this.addCommand(pressright);
		mensaje1="click 3";
		break;
	case 3:	//exit
		midlet.notifyDestroyed();
		break;
	case 4:	//pressleft
		leftispressed=true;
		this.removeCommand(pressleft);
	    this.addCommand(releaseleft);
		mensaje1="press 1";
		break;
	case 5:	//releaseleft
		leftispressed=false;
		this.removeCommand(releaseleft);
	    this.addCommand(pressleft);
		mensaje1="release 1";
		break;
	case 6:	//pressright
		rightispressed=true;
		this.removeCommand(pressright);
	    this.addCommand(releaseright);
		mensaje1="press 3";
		break;
	case 7:	//releaseleft
		leftispressed=false;
		this.removeCommand(releaseright);
	    this.addCommand(pressright);
		mensaje1="release 3";
		break;

	default:
		break;
	}
	send(mensaje1);

}
public void clicked(int x, int y){
	if((!leftispressed)&&(!rightispressed)){;
	    //control de la pantalla completa
	    if ((x <= getHeight()/100.0*fullscreenpercent )&&(y <=getHeight()/100.0*fullscreenpercent)&&
	    		(relx <= getHeight()/100.0*fullscreenpercent )&&(rely <=getHeight()/100.0*fullscreenpercent)){
	    	this.setFullScreenMode(!fullscreen);
	    	fullscreen=!fullscreen;
	    }
		send("click 1");
	}

}
public  void send(String mensaje){
	final String tosend=mensaje;
	try
	{
	    	new Thread(new Runnable()
	    	{
	    	public void run()
	    	{
	    	try{
	    	sender.send2(tosend);
	    	} catch(Exception e) {}
	    	}
	    	}).start();
	}
	catch (Exception e)
	{
	  	System.out.println(e.toString());
	}
}
public int getCode(Command c){
	int key=0;
	if(c == exit){
		key=3;
	}else if(c == right){
		key=2;
	}else if(c == left){
		key=1;
	}else if(c == pressleft){
		key=4;
	}else if(c == releaseleft){
		key=5;
	}else if(c == pressright){
		key=6;
	}else if(c == releaseright){
		key=7;
	}
	return key;
}


}
